#pragma once
#include <sys/epoll.h>
#include"log.hpp"


//封装的epoll
class Epoller {
    public:
        Epoller() {}
        void Init()
        {
            _epollfd = epoll_create(256);
            if(_epollfd < 0){
                LOG(log_level::ERROR) << "epoll_create error";
                exit(1);
            }
            LOG(log_level::INFO) << "epoll_create success fd: "<<_epollfd;
        }
        int Wait(struct epoll_event *events, int maxevents, int timeout)//输出就绪的fd和events
        {
            int n = epoll_wait(_epollfd, events, maxevents, timeout);
            if(n < 0){
                LOG(log_level::WARNING) << "epoll_wait error";
            }
            return n;
        }
        void Ctrl(int fd, uint32_t events,int op)
        {
            struct epoll_event ev;
            ev.events = events;
            ev.data.fd = fd;
            int n = epoll_ctl(_epollfd, op, fd, &ev);
            if(n < 0){
                LOG(log_level::WARNING) << "epoll_ctl error";
            }
        }
        ~Epoller() {}
    private:
        int _epollfd = -1;
};